package a630wj.recycleview630wj.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.activity.ArticleDetailActivity;
import a630wj.recycleview630wj.adapter.ArticleAdapter;
import a630wj.recycleview630wj.adapter.CourseAdapter;
import a630wj.recycleview630wj.bean.CourseBean;
import a630wj.recycleview630wj.iface.CourseListener;
import a630wj.recycleview630wj.model.CourseModel;

/**
 * Created by weijie on 2017/6/6.
 */

public class CourseFragment extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private CourseAdapter courseAdapter;
    private List<CourseBean>  list = null;
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.coursefragment, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        context = getActivity();

        initRecyclerView(view);
        CourseModel courseModel = new CourseModel();
        courseModel.getResultList("course", 2, "8soafn3tpaj0efuab5jvmpbhr4", courseListener);
        //return view;
    }

    CourseListener courseListener = new CourseListener() {
        @Override
        public void onResponse(List<CourseBean> listbean) {
            list=listbean;
            courseAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void initRecyclerView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_article);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(context);
        //设置布局
        recyclerView.setLayoutManager(layoutManager);
        //设置item的高度
        recyclerView.setHasFixedSize(true);
        //创建适配器
//        list=getData();
        courseAdapter= new CourseAdapter(context,list);
        ArticleAdapter.OnItemClickListener onItemClickListener = new ArticleAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ArticleDetailActivity.class);
                intent.putExtra("resid", list.get(position).getId());
                startActivity(intent);

            }
        };
        courseAdapter.setList(list);
        recyclerView.setAdapter(courseAdapter);
        recyclerView.setAdapter(courseAdapter);
    }

}